package com.example.hospital.outpatientservicecharge.controller;

import com.example.hospital.outpatientservicecharge.pojo.Message;
import com.example.hospital.outpatientservicecharge.pojo.Name;
import com.example.hospital.outpatientservicecharge.pojo.User;
import com.example.hospital.outpatientservicecharge.repository.MedicineRepository;
import com.example.hospital.outpatientservicecharge.repository.MessageRepository;
import com.example.hospital.outpatientservicecharge.repository.NameRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class testcotroller {
    @Autowired
    private MessageRepository messageRepository;
    @Autowired
    private NameRepository nameRepository;
    @Autowired
    private MedicineRepository medicineRepository;

    boolean is_start=true;
    static int allmoney=0;
    static String[] nn=new String[10];
    static int[] mm=new int[10];
    static boolean is_tianjia=false;
    static String name=null;
    static int x=0;
    static int howmany=0;
    @PostMapping("/xiumu")
    public String getDbType(User user,Map map,String content){


        List<Message> messages = messageRepository.findAll();
        List<Name> names = nameRepository.findAll();
        Message message;



        if(content!=null) {
            if(messageRepository.findByMid(content)!=null) {
                name=content;
                message = messageRepository.findByMid(content);
            }
            else
                message=messageRepository.getOne("默认");
        }
        else
        {
            message=messageRepository.getOne("默认");
        }

        Name name1;

        //Medicine medicine = medicineRepository.getOne(1);

        if(user.getUsername()!=null) {
            name1 = nameRepository.getOne(user.getUsername());
        }
        else
        {
            name1 = nameRepository.getOne("1");
        }

        //System.out.println("用户名："+user.getUsername()+"\t密码："+user.getPassword());
        if(user.getUsername()!=null) {
            if ((user.getUsername().compareTo(name1.getId()) == 0) && (user.getPassword().compareTo(name1.getPassword()) == 0)) {
                map.put("msg", user.getPassword());
            } else {
                map.put("msg", 3);
            }
        }

        if(is_tianjia)
        {
            map.put("ctr1",5);
        }

        int i=0;

        if(user.getFname()!=null&&user.getFname().compareTo("Mickey")==0)
        {
            is_start=true;
        }
        if(is_start) {
            for (i = 0; i < 10; i++) {
                nn[i] = null;
                mm[i] = 0;
            }
            i=0;
            for(Message message1 : messages) {
                i++;
                message1.setAllnum(mm[i]);
            }
            messageRepository.save(message);
            is_start=false;
        }

        if(content!=null) {

            if (content.compareTo(message.getMid()) == 0) {
                map.put("nameid",message.getMid() );
                map.put("costid",message.getLastpay());
                map.put("ctr", 2);
            }
            else
            {
                map.put("ctr", 3);
            }
        }
        return "index";
    }



    @PostMapping("/xiumu1")
    public String getDbType1(User user,Map map) {
        List<Message> messages = messageRepository.findAll();
        Message message1;
        message1 = messageRepository.findByMid(name);

        map.put("nameid",message1.getMid() );
        map.put("costid",message1.getLastpay());


        int i=0;
        if(user.getSname()!=null&&user.getSname().compareTo("tianjia") == 0)
        {
            is_tianjia=true;

            user.setHowhuch(5);

            i = 0;
            for (Message message2 : messages) {
                //strcpy(nn[i], message2.getMid());
                int zz=message2.getMid().length();
                nn[i]=message2.getMid().substring(0,zz);

            //mm[i]++;
            i++;
            }
            for(i=0;i<10;i++) {
            if(nn[i]!=null&&message1.getMid().compareTo(nn[i])==0) {
                mm[i]++;
                howmany++;
                message1.setAllnum(mm[i]);
            }
            }
            messageRepository.save(message1);
            i = 0;
            for (Message message2 : messages) {
                x = x + (message2.getAllnum() * message2.getLastpay());
            }
            map.put("need",message1.getAllnum());
            map.put("money",x);
            map.put("list",message1);
            x=0;
            map.put("ctr1", 5);
        }

        if(is_tianjia)
        {
            map.put("ctr1",5);
        }
        return "index";
    }



    @RequestMapping("/lx")
    public String pay(Map map){
        List<Message> messages = messageRepository.findAll();
        List<Message> messages1=new ArrayList<>();
        for (Message message2 : messages) {
            if(message2.getAllnum()!=0)
            {
                messages1.add(message2);
            }
        }
        map.put("tre",messages1);

        for (Message message2 : messages) {
            x = x + (message2.getAllnum() * message2.getLastpay());
        }
        map.put("try",x);
        x=0;
        return "pay";
    }
    @RequestMapping("/lxx")
    public String fanhui()
    {
        return "index";
    }


    @PostMapping("/lxxx")
    public String shanchu(User user)
    {
        List<Message> messages = messageRepository.findAll();

        for (Message message2 : messages) {
            if(message2.getMid().compareTo(user.getTname()) == 0)
            {
                message2.setAllnum(message2.getAllnum()-1);//Integer.parseInt(user.getTcost()));


            int i;
            for(i=0;i<10;i++) {
                if(nn[i]!=null&&message2.getMid().compareTo(nn[i])==0) {
                    mm[i]--;
                    howmany--;
                    message2.setAllnum(mm[i]);
                }
            }
            }
            messageRepository.save(message2);
        }
        return "index";
    }

    @PostMapping("/xiumu2")
    public Map add(Map map){
        List<Message> messages = messageRepository.findAll();
        List<Message> messages1=new ArrayList<>();
        /*for (Message message2 : messages) {
            if (message2.getMid().compareTo(sname) == 0) {
                message2.setAllnum(message2.getAllnum() - 1);//Integer.parseInt(user.getTcost()));
            }
        }*/


        String l="sds";
        map.put("try",x);


        return map;
    }
}
